<template>
  <div class="search-root">
    <el-form :model="searchForm" ref="searchFormRef" label-width="80">
      <el-row :gutter="80">
        <el-col :span="12">
          <el-form-item label="用户名称" prop="userName">
            <el-input
              placeholder="请输入用户名称"
              v-model="searchForm.userName"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-select
              placeholder="全部"
              v-model="searchForm.gender"
              style="width: 100%"
              clearable
            >
              <el-option
                v-for="item in genderList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="search-operator">
      <el-button type="primary" @click="query">查询</el-button>
      <el-button type="primary" plain @click="reset(searchFormRef)"
        >重置</el-button
      >
    </div>

    <div class="operator">
      <el-button type="primary" @click="deleteBatch">批量删除</el-button>
      <el-button type="primary" plain @click="add">新增</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { reactive, ref } from 'vue'

const $emit = defineEmits(['showDialog', 'deleteBatch', 'query'])

interface SearchType {
  userName: string
  gender: null | string
}

const searchForm = reactive<SearchType>({
  userName: '',
  gender: null
})

const searchFormRef = ref<FormInstance>()

const genderList = ref<any[]>([
  { value: 1, label: '男' },
  { value: 2, label: '女' }
])

const query = () => {
  $emit('query', { ...searchForm })
}
const reset = (formEl: FormInstance | undefined) => {
  if (!formEl) return

  formEl.resetFields()
}
const deleteBatch = () => {
  $emit('deleteBatch')
}
const add = () => {
  $emit('showDialog')
}
</script>

<style lang="scss" scoped>
.search-root {
  // width: 100%;
  .search-operator {
    text-align: right;
  }
}
</style>
